package vn.edu.nuce.bean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.TabChangeEvent;

import vn.edu.nuce.model.LichDangKy;
import vn.edu.nuce.service.LichDangKyService;
import vn.edu.nuce.utility.Utility;

@ManagedBean(name = "lichSuBean")
@ViewScoped
public class LichSuBean {
	@ManagedProperty(value = "#{LichDangKyService}")
	LichDangKyService lichDangKyService;

	private Date date;
	private Date fromDate;
	private Date toDate;
	private int thangSelected;
	private int namSelected;
	private int namSelected2;
	private String strMonHoc;
	private String userId;
	private String ngayHienThi;
	List<Integer> thangList;
	List<Integer> namList;
	List<LichDangKy> lichDangKyList;

	public LichSuBean() {
		userId = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.AUTH_ID)+"";
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		thangList = new ArrayList<Integer>();
		namList = new ArrayList<Integer>();
		int year = Calendar.getInstance().get(Calendar.YEAR);
		for (int i = 0; i < 12; i++) {
			namList.add(year--);
			thangList.add(i + 1);
		}
		try {
			Date dateWithoutTime = sdf.parse(sdf.format(new Date()));
			date = dateWithoutTime;
			fromDate = dateWithoutTime;
		} catch (ParseException e) {
			date = new Date();
			fromDate = new Date();
			e.printStackTrace();
		}
		ngayHienThi = Utility.formatDate(date);
	}

	public void handleDateSelect(SelectEvent event) {
		date = (Date) event.getObject();
		getListFromDate();
	}

	public void handleWeekSelect(SelectEvent event) {
		fromDate = (Date) event.getObject();
		getListFromWeek();
	}

	

	private void getListFromDate() {
		lichDangKyList = getLichDangKyService().getLichDangKyByDateAndUser(
				date, userId,"");
		ngayHienThi = Utility.formatDate(date);
	}

	private void getListFromWeek() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(fromDate);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		fromDate = cal.getTime();
		cal.add(Calendar.DATE, 6);
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, userId,"");
		ngayHienThi = Utility.formatDate(fromDate) + " - " + Utility.formatDate(toDate);
	}

	public void getListFromMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(namSelected, thangSelected - 1, 1);
		fromDate = cal.getTime();
		cal.set(namSelected, thangSelected - 1,
				cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, userId,"");
		ngayHienThi = thangSelected + "/" + namSelected;
	}

	public void getListFromYear() {
		Calendar cal = Calendar.getInstance();
		cal.set(namSelected2, 0, 1);
		fromDate = cal.getTime();
		cal.set(namSelected2, 11, 31);
		toDate = cal.getTime();
		lichDangKyList = getLichDangKyService().getLichDangKyBy2DateAndUser(
				fromDate, toDate, userId,"");
		ngayHienThi = namSelected2 + "";
	}

	public void getListFromMonHoc(){
		lichDangKyList = getLichDangKyService().getLichDangKyTheoMonHocAndUser(strMonHoc,userId,"");
		ngayHienThi = "";
	}

	public void onTabChange(TabChangeEvent event) {
		if (event.getTab().getId().equals("tab1")) {
			getListFromDate();
		} else if (event.getTab().getId().equals("tab2")) {
			getListFromWeek();
		} else if (event.getTab().getId().equals("tab3")) {
			getListFromMonth();
		} else if (event.getTab().getId().equals("tab4")) {
			getListFromYear();
		} else if (event.getTab().getId().equals("tab5")) {
			getListFromMonHoc();
		}
	}

	public List<LichDangKy> getLichDangKyList() {
		if (lichDangKyList == null) {
			lichDangKyList = getLichDangKyService().getLichDangKyByDateAndUser(
					fromDate, userId,"");
		}
		return lichDangKyList;
	}

	public void setLichDangKyList(List<LichDangKy> lichDangKyList) {
		this.lichDangKyList = lichDangKyList;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public List<Integer> getThangList() {
		return thangList;
	}

	public void setThangList(List<Integer> thangList) {
		this.thangList = thangList;
	}

	public List<Integer> getNamList() {
		return namList;
	}

	public void setNamList(List<Integer> namList) {
		this.namList = namList;
	}

	public LichDangKyService getLichDangKyService() {
		return lichDangKyService;
	}

	public void setLichDangKyService(LichDangKyService lichDangKyService) {
		this.lichDangKyService = lichDangKyService;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getNgayHienThi() {
		return ngayHienThi;
	}

	public void setNgayHienThi(String ngayHienThi) {
		this.ngayHienThi = ngayHienThi;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public int getThangSelected() {
		return thangSelected;
	}

	public void setThangSelected(int thangSelected) {
		this.thangSelected = thangSelected;
	}

	public int getNamSelected() {
		return namSelected;
	}

	public void setNamSelected(int namSelected) {
		this.namSelected = namSelected;
	}

	public int getNamSelected2() {
		return namSelected2;
	}

	public void setNamSelected2(int namSelected2) {
		this.namSelected2 = namSelected2;
	}

	public String getStrMonHoc() {
		return strMonHoc;
	}

	public void setStrMonHoc(String strMonHoc) {
		this.strMonHoc = strMonHoc;
	}

}
